// app.js

import {
  getSession
} from './utils/network/api'

App({
  onLaunch() {
    console.log('-------------onLaunch------------------');
    //获取设备信息
    wx.getSystemInfo({
      success: (res) => {
        this.globalData.systemInfo.statusBarHeight = res.statusBarHeight
        this.globalData.systemInfo.windowHeight = res.windowHeight
        console.log('hm--状态栏的高度，单位px->', res.statusBarHeight)
        console.log('hm--可使用窗口高度，单位px->', res.windowHeight)
      }
    })

    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    //获取session
    const params = new Map()
    params.set('timestamp', Date.now())
    params.set('signature', '#QWEdf^&589+')
    params.set('userAgent', "")
    getSession(params).then(response => {
        if (response.code === 1000) {
          //获取sessionid
          const sessionId = response.data.sessionId;
          getApp().globalData.userInfo.sessionId = sessionId;
        } else {
          console.log('--getSession-->', response)
          wx.showToast({
            title: 'session失败',
            icon: 'error',
            duration: 2000
          })
        }

      })
      .catch(error => {
        console.log('--getSession-->error', error.errMsg)
        wx.showToast({
          title: 'session失败',
          icon: 'error',
          duration: 2000
        })
      });

    // 登录
    wx.login({
      success: (res) => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  globalData: {
    systemInfo: {
      statusBarHeight: 0,
      windowHeight: 0,
    },
    userInfo: {
      sessionId: '',
      userName: '张三',
      age: 19,
    }
  }
})